package com.transaction.simulate.mapper;

import com.transaction.simulate.model.dto.Menu;
import com.transaction.simulate.model.dto.RoleMenu;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MenuMapper {

    @Select("SELECT * FROM t_menu t WHERE EXISTS ( SELECT 1 FROM t_roleMenu rm WHERE rm.roleCode IN ( SELECT ur.roleCode FROM t_userRole ur WHERE ur.userCode = #{userCode} ) AND rm.menuCode = t.menuCode ) ORDER BY t.menuOrder")
    List<Menu> selectAllMenuByUserCode(String userCode);

    @Insert("INSERT INTO t_menu (menuCode, parentCode, menuName, menuUrl, menuIcon , menuOrder) " +
            "VALUES (#{menuCode},#{parentCode},#{menuName},#{menuUrl},#{menuIcon},#{menuOrder})")
    void addMenu(Menu menu);

    @Select("SELECT t.menuCode AS id, t.parentCode AS parentId, t.menuName AS label FROM t_menu t ORDER BY t.menuOrder")
    List<RoleMenu> selectAllMenu();

    @Select("select t.menuCode from t_roleMenu t where t.roleCode = #{roleCode} ORDER BY id ")
    List<String> selectMenusByRoleCode(String roleCode);

    @Delete("delete from t_roleMenu where roleCode = #{roleCode}")
    void deleteRoleMenuByRoleCode(String roleCode);
}
